#include <iostream>
#include <algorithm>

template <typename Object>
class Vector{
    private:
        int theSize;        //vector的现在长度
        int theCapacity;    //vector剩余容量
        Object* objects;   //vector数组的指针
    
    public:
        // 备用长度
        static const int SPARE_CAPACITY  = 16;

        // 构造函数：接受一个长度参数，构造一个对象数组
        Vector( int initSize = 0) : theSize{initSize}, theCapacity { initSize + SPARE_CAPACITY } 
        {
            objects = new Object[ theCapacity];      //列表初始化：首先将 设定长度initSize 加上 备用长度，得到对象数组总长度
        }
        
        // 左值复制构造函数：接受一个Vector只读引用，并进行移动语义
        Vector( const Vector & rhs) : theSize{ rhs.theSize }, theCapacity{ rhs.theCapacity }, objects{ nullptr }
        {
            objects = new Object[ theCapacity ];
            for( int k = 0; k < theSize; ++k)
                objects[ k ] = rhs.objects[k];  //这里调用的是重载的左值赋值运算符
        }

        // 左值赋值运算符（swap的封装）（本质还是移动语义）
        Vector & operator= (const Vector & rhs)
        {
            Vector copy = rhs;
            std::swap(*this, copy);
            return *this;
        }

        // 析构函数（delete的封装）
        ~Vector()
        { delete[] objects; }

        // 右值复制构造函数：移动语义
        Vector( Vector && rhs ) : theSize {rhs.theSize}, theCapacity{ rhs.theCapacity }, objects{ rhs.objects }
        {
            rhs.objects = nullptr;
            rhs.theSize = 0;
            rhs.theCapacity = 0;
        }
        
        // 右值赋值运算符
        Vector & operator= (Vector && rhs)
        {
            std::swap( theSize, rhs.theSize );
            std::swap( theCapacity, rhs.theCapacity);
            std::swap( objects, rhs.objects );

            return *this;
        }

        // 如果新长度大于容量，则把容量变成2倍后再赋值新长度
        void resize( int newSize)
        {
            if (newSize > theCapacity)
                reserve( newSize * 2);
            theSize = newSize;
        }

        // 预先分配空间
        void reserve( int newCapacity  )
        {
            if( newCapacity < theSize )     //若分配空间还没有原先的大，则终止
                return;
            
            /*我以为会末尾添加，没想到直接右值迁移到一个更大的数组中去了……*/
            Object * newArray = new Object[ newCapacity ];      //初始化新数组
            for( int k = 0; k < theSize; ++k )                  //使用move迁移
                newArray[k] = std::move( objects[k] );

            theCapacity = newCapacity;                          //重新定义容量属性
            std::swap( objects, newArray );                     //交换其它属性
            delete [] newArray;
        }

        // 索引运算符
        Object & operator[] (int index)
            { return objects[ index ]; }
        const Object & operator[] (int index) const
            { return objects[ index ]; }

        // 判空、长度、容量
        bool empty() const
            { return size() == 0; }
        int size() const
            { return theSize; }
        int capacity() const
            { return theCapacity; }
        
        // 左值元素添加到末尾
        void push_back(const Object & x)
        {
            if( theSize == theCapacity )
                reserve(2 * theCapacity + 1);
            objects[ theSize++ ] = x;
        }

        // 右值元素添加到末尾
        void push_back(Object && x)
        {
            if( theSize == theCapacity )
                reserve(2 * theCapacity + 1);
            objects[ theSize++ ] = x;
        }

        // 移除末尾元素（停止保护末尾元素的内存）
        void pop_back()
        {
            --theSize;
        }

        // 读取末尾元素
        const Object & back() const
        {
            return objects[ theSize - 1 ];
        }

        /*迭代器设置*/
        typedef Object* iterator;
        typedef const Object* const_iterator;

        iterator begin()
            { return &objects[0]; }
        const_iterator begin() const
            { return &objects[0]; }
        iterator end()
            { return &objects[ size() ]; }
        const_iterator end() const
            { return &objects[ size() ]; }
        
};

int main(){
    Vector<int> vec(10);
    for (int i : vec)
        vec.push_back(i);
    std::cout  << "vec.size()是" << vec.size() << std::endl;
    std::cout  << "vec.thesize 是" << "啊这个人" << std::endl;
    std::cout  << "vec.theCapacity是" << vec.capacity() << std::endl;
    
    return 0;
}